<?php
namespace app\admin\controller\house;

use app\admin\controller\Common;
use app\admin\model\HouseProperty as PropertyModel;
use app\admin\model\HouseTenant as TenantModel;
use app\admin\model\HouseNumber as NumberModel;
use app\admin\library\Property;

class Tenant extends Common
{
    public function index()
    {
        $loginUser = $this->auth->getLoginUser();
        $house_property_id = $this->request->param('house_property_id/d', Property::getProperty($loginUser['id']));
        $properties = PropertyModel::where('admin_user_id', $loginUser['id'])->all();
        $property = [];
        foreach ($properties as $v) {
            if ($v['id'] === $house_property_id) {
                $property = $v;
                break;
            }
        }
        $tenants = TenantModel::where('a.house_property_id', $house_property_id)
        ->alias('a')
        ->join('HouseNumber b', 'a.house_property_id = b.house_property_id and a.house_number_id = b.id')
        ->field("a.*,b.name as number_name, date_format(now(), '%Y') - substring(a.id_card_number,7,4) as age")
        ->order(['leave_time','number_name'])
        ->paginate(10);
        $this->assign('property', $property);
        $this->assign('properties', $properties);
        $this->assign('house_property_id', $house_property_id);
        $this->assign('tenants', $tenants);
        return $this->fetch();
    }

    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        $house_property_id = $this->request->param('house_property_id/d', 0);
        $property = PropertyModel::get($house_property_id);
        $data = [
            'house_property_id' => $house_property_id,
            'house_property_name' => $property['name'],
            'house_number_id' => '',
            'name' => '',
            'sex' => '',
            'phone' => '',
            'id_card_number' => '',
            'native_place' => '',
            'work_units' => '',
            'checkin_time' => '',
        ];
        if ($id) {
            if (!$data = TenantModel::get($id)) {
                $this->error('记录不存在。');
            }
            $data['house_property_name'] = $property['name'];
        }
        $number = NumberModel::where('house_property_id', $house_property_id)->all();
        $this->assign('number', $number);
        $this->assign('data', $data);
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function save()
    {
        $id = $this->request->post('id/d', 0);
        $data = [
            'house_property_id' => $this->request->post('house_property_id/d', 0),
            'house_number_id' => $this->request->post('house_number_id/d', 0),
            'name' => $this->request->post('name/s', '', 'trim'),
            'sex' => $this->request->post('sex/s', '', 'trim'),
            'phone' => $this->request->post('phone/s', '', 'trim'),
            'id_card_number' => $this->request->post('id_card_number/s', '', 'trim'),
            'native_place' => $this->request->post('native_place/s', '', 'trim'),
            'work_units' => $this->request->post('work_units/s', '', 'trim'),
            'checkin_time' => $this->request->post('checkin_time/s', \date('Ymd'), 'trim'),
        ];
        if ($id) {
            if (!$tenant = TenantModel::get($id)) {
                $this->error('修改失败，记录不存在');
            }
            $tenant->save($data);
            $this->success('修改成功');
        }
        TenantModel::create($data);
        $this->success('添加成功');
    }
}
